package sz.szsmk.citizencard.ui.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.chenenyu.router.annotation.Route;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.UserAddNewcardBinding;
import sz.szsmk.citizencard.dialog.OneButtonProDialog;
import sz.szsmk.citizencard.dialog.SuccessDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.mvp.contract.IUserAddNewCardContact;
import sz.szsmk.citizencard.mvp.presenter.UserAddNewsCardPresenter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/16 13:46
 * description:首页 我的绑卡页面
 */
@Route("addnewcard")
public class UserAddNewsCardActivity extends IBaseActivity<UserAddNewsCardPresenter, UserAddNewcardBinding>
        implements IUserAddNewCardContact.IUserAddNewCardView, View.OnClickListener {

    private Dialog dialog;
    private View inflate;
    private String key;
    private EditText etId;
    private EditText etName;
    private String etCard;
    private String etNick;
    private SuccessDialog dialog1;
    private boolean isAcard;

    @Override
    protected int attachLayoutRes() {
        return R.layout.user_add_newcard;
    }

    @Override
    protected void initInjector() {

        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        key = getIntent().getStringExtra("key");

        dataBinding.titleLayout.settingTitle.setText("添加卡号");

        dataBinding.titleLayout.back.setOnClickListener(this);
        dataBinding.userAddSure.setOnClickListener(this);
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void updataError() {

    }

    @Override
    public void getNewsInfoSucc() {

    }

    @Override
    public void bindFaile(String message) {
        PubUtils.popTipOrWarn(this, message);
    }

    @Override
    public void bindHasAcard(String message) {
        OneButtonProDialog passwordDialog = new OneButtonProDialog();
        passwordDialog.setAction(1);
        passwordDialog.setContent(message);
        passwordDialog.setTitle("苏州市民卡");
        passwordDialog.setCancelText("我知道了");
        passwordDialog.setOkText("我是确定");
        passwordDialog.setOnCancelClickListener(new OneButtonProDialog.OnCancelClickListener() {
            @Override
            public void onCancelClick() {
//                Toast.makeText(BankAdminActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        passwordDialog.setOnOkClickListener(new OneButtonProDialog.OnOkClickListener() {
            @Override
            public void onOkClick() {
//                Toast.makeText(BankAdminActivity.this, "开启", Toast.LENGTH_SHORT).show();
            }
        });
        passwordDialog.show(this.getFragmentManager(), null);
    }

    @Override
    public void bindSuccess() {
        dialog1 = new SuccessDialog(this, "绑定成功");

        dialog1.show();
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                dialog1.cancel();
                finish();
            }
        }.start();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.user_add_sure) {
            etCard = dataBinding.addCardIdEt.getText().toString().trim();
            etNick = dataBinding.addCardNickEt.getText().toString().trim();

            if (etCard == null || "".equals(etCard)) {
                Toast.makeText(this, "请输入正确的信息", Toast.LENGTH_LONG).show();
                return;
            }
            if (etNick == null || "".equals(etNick)) {
                Toast.makeText(this, "请输入正确的信息", Toast.LENGTH_LONG).show();
                return;
            }
            String start = etCard.substring(0, 1);
            String end = etCard.substring(etCard.length() - 1, etCard.length());
            String substring = etCard.substring(1, etCard.length() - 1);
            String num = etCard.substring(1, etCard.length());
            String type = "0";
            if (start.equals("A") || start.equals("a")) {
                if (end.equals("X") && isNumeric(substring) && etCard.length() == 9) {
                    type = "1";
                }
                if (end.equals("x") && isNumeric(substring) && etCard.length() == 9) {
                    type = "1";
                }
            }


            if (start.equals("A") || start.equals("a")) {
                if (isNumeric(num) && etCard.length() == 9) {
                    isAcard = true;
                } else {
                    isAcard = false;
                }
            } else {
                isAcard = false;
            }
            if (TextUtils.isEmpty(etCard)) {
                PubUtils.popTipOrWarn(this, "请输入一个卡号");

                return;
            }
//                if (key.equals("user")) {
//                    if (etCard.length() > 16) {
//                        PubUtils.popTipOrWarn(this, "请输入正确的卡号");
//                        return;
//                    }
//                }
            if (TextUtils.isEmpty(etNick)) {
                PubUtils.popTipOrWarn(this, "请输入一个昵称");
                return;
            }
            if (isAcard || type.equals("1")) {

                showAcardDialog();
            } else {
                mPresenter.bindCard(etCard, etNick, "", "");
            }
//                showMydialog();
        }
    }

    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    private void showAcardDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View dialogView = getLayoutInflater().inflate(R.layout.acard_dialog, null);
        builder.setView(dialogView);
        Button acardCancel = (Button) dialogView.findViewById(R.id.acard_cancel);
        etId = (EditText) dialogView.findViewById(R.id.acard_id_et);
        etName = (EditText) dialogView.findViewById(R.id.acard_name_et);

        final AlertDialog dialog = builder.create();
        acardCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        Button acardSure = (Button) dialogView.findViewById(R.id.acard_sure);
        acardSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = etName.getText().toString();
                String id = etId.getText().toString();
                if (TextUtils.isEmpty(id)) {
                    Toast.makeText(UserAddNewsCardActivity.this, "请输入持卡人姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(UserAddNewsCardActivity.this, "请输入持卡人证件", Toast.LENGTH_SHORT).show();
                    return;
                }
                mPresenter.bindCard(etCard, etNick, name, id);

            }
        });
        dialog.show();
    }

    private void showMydialog() {
        dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        inflate = LayoutInflater.from(this).inflate(R.layout.dialog_add_card, null);
        dialog.setContentView(inflate);
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setAttributes(lp);
        dialog.show();//显示对话框
    }
}
